/*
 * @Author: zy
 * @Date: 2021-10-25 17:20:19
 * @Last Modified by: zy
 * @Last Modified time: 2021-10-27 15:37:14
 */

import { request } from 'umi';

type CommonResponseType = {
  code: number;
  message: string;
};

// 1、获取分类列表
type ClassificationListDataType = {
  code: number;
  message: string;
  data: {
    rowNumber: number;
    list: ClassificationListItemType[] | [];
  };
};

type ClassificationListItemType = {
  aid: number;
  name: string;
  isDelete: boolean;
  children: ClassificationListItemType[] | [];
};

// 1.2、获取分类列表带查询条件
type QueryParamsType = {
  searchKey?: string;
  page: number;
  pageSize: number;
};

// 3、新增修改时获取分类信息
export type OptionClassificationInfoDataType = {
  code: number;
  message: string;
  data: {
    name: string;
    fatherAid: string;
    fatherName: string;
  };
};

// 4、新增修改时获取类型列表
type OptionClassificationTypesDataType = {
  code: number;
  message: string;
  data: ClassificationItem[] | [];
};

export type ClassificationItem = {
  aid: number;
  name: string;
  children: ClassificationItem[] | [];
};

export type TreeDataType = {
  value: number;
  title: string;
  children: TreeDataType[] | [];
};

// 5.1、新增修改时检验父类传给后端
type OptionFatherParamsType = {
  aid: number;
  fatherAid: number;
};

// 5.2、新增修改时检验父类后端传回来数据
type OptionFatherDataType = {
  code: number;
  message: string;
  data: {
    isDescendant: boolean;
  };
};

// 6、新增修改时保存分类
type OptionSaveClassificationBodyType = {
  aid: number;
  name: string;
  fatherAid?: number;
};

// 1、请求获取分类列表
export async function getClassificationList(params: QueryParamsType) {
  return request<ClassificationListDataType>(`/classificationManage/getClassificationList`, {
    method: 'GET',
    params,
  });
}

// 2、请求删除分类
export async function delClassification(body: { aid: number }) {
  return request<CommonResponseType>(`/classificationManage/delClassification`, {
    method: 'POST',
    body: JSON.stringify(body),
  });
}

// 3、请求新增修改时获取分类信息
export async function getClassificationInfo(params: { aid: number }) {
  return request<OptionClassificationInfoDataType>(`/classificationManage/getClassificationInfo`, {
    method: 'GET',
    params,
  });
}

// 4、请求新增修改时获取类型列表
export async function getClassificationTypes() {
  return request<OptionClassificationTypesDataType>(`/classificationManage/getTypeList`, {
    method: 'GET',
  });
}

// 5、请求检验父类
export async function checkFatherClassification(params: OptionFatherParamsType) {
  return request<OptionFatherDataType>(`/classificationManage/checkFatherClassification`, {
    method: 'GET',
    params,
  });
}

// 6、请求保存分类
export async function saveClassification(body: OptionSaveClassificationBodyType) {
  return request<CommonResponseType>(`/classificationManage/saveClassification`, {
    method: 'POST',
    body: JSON.stringify(body),
  });
}
